package edu.java.lectures.lec14.collections.iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

// The Iterator is a design pattern as well.
public class IteratorTest {

    private static final String LINE = "-----------------------";

    public static void main(String[] args) {
	List<String> myStrList = new ArrayList<String>();

	myStrList.add("ala");
	myStrList.add("bala");
	myStrList.add("nica");
	myStrList.add("turska");
	myStrList.add("panica");

	Iterator<String> myStrListIter = myStrList.iterator();

	// works but is not secure
	// System.out.println(LINE);
	// System.out.println(myStrListIter.next());
	// System.out.println(myStrListIter.next());
	// System.out.println(myStrListIter.next());
	// System.out.println(myStrListIter.next());
	// System.out.println(myStrListIter.next());
	// System.out.println(myStrListIter.next());

	System.out.println(LINE);
	// classic :D
	while (myStrListIter.hasNext()) { // 1. hasNext()

	    // 2. next() -> after hasNext()
	    System.out.println(myStrListIter.next());

	    // 3. [optional] remove() -> ALWAYS after next()
	    myStrListIter.remove();
	}
	myStrListIter.remove();
	// myStrListIter.remove();
	System.out.println(myStrList);

	System.out.println(LINE);
	// not possible - the iterator has "expired" -> i.e. can NOT be "REUSED"
	// needs restart, i.e.:
	// myStrListIter = myStrList.iterator();
	while (myStrListIter.hasNext()) {
	    System.out.println(myStrListIter.next());
	    // myStrList.remove(3); // wrong APPROACH
	}

    }
}
